package cn.submarine;
import java.awt.*;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.DebugGraphics;
public abstract class SeaObject {
    public static final int LIVE = 0;//对象状态
    public static final int DEAD = 1;
    protected int state = LIVE;//当前状态


    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;
    //此构造固定给潜艇（侦查潜艇，鱼雷潜艇，水雷潜艇）使用
    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT - height - 150 + 1) + 150;
        speed = rand.nextInt(3) + 1;
    }

    // 此构造方法固定给战舰、炸弹、水雷使用
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height =height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public abstract void move ();//海洋对象移动

    public abstract ImageIcon getImage();//获取对象的图片


    //判断对象是否活着
    public boolean isLive(){
        return state == LIVE;//返true，或false
    }

    public boolean isDead(){
        return state == DEAD;
    }

    public void paintImage(Graphics graphics){
        if (this.isLive()){//若活着
            this.getImage().paintIcon(null,graphics,this.x,this.y);
        }
    }

    public boolean isOutBounds(){
        return this.x >= World.WIDTH;//超过WIDTH的长度即为越界
    }

    public boolean isHit(SeaObject other){
        // 假设this指代潜艇，other指代炸弹
        int x1 = this.x-other.width;
        int x2 = this.x+this.width;
        int y1 = this.y-other.height;
        int y2 = this.y+this.height;
        int x = other.x;//炸弹的x
        int y = other.y;//炸弹的y
        return (x>=x1 && x<=x2) && (y>=y1 && y<=y2);//在此范围即为撞上
    }

    public void goDead(){//修改海洋对象默认状态为死，则world类画对象时不再画
        state = DEAD;
    }

}
